//
//  S1mpl3LittleViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "S1mpl3LittleViewController.h"
#import "TwoToOne.h"
#import "MJRefresh.h"
#import "FitHeader.h"
#import "S1mpl3Album.h"
#import "S1mpl3RecommendTableViewCell.h"
#import "TabBarController.h"
#import "S1mpl3AlbumViewController.h"
#import "S1mpl3AlbumTableViewCell.h"

@interface S1mpl3LittleViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSourceArray;
@property(nonatomic,strong)UISegmentedControl *segmentedControl;
@end

@implementation S1mpl3LittleViewController

-(void)getData{
//    NSLog(@"%@", [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/tags/get_albums?device=iPhone&page=1&sort=hot&tname=%@", _titleString]);
    if (_segmentedControl.selectedSegmentIndex == 1) {
        [TwoToOne getDataByString:[NSString stringWithFormat:@"http://mobile.ximalaya.com/m/tags/get_albums?device=iPhone&page=%ld&sort=hot&tname=%@", _page, _titleString] BodyString:nil WithDataBlock:^(id data) {
            NSArray *listArray = [data objectForKey:@"list"];
            if (listArray.count == 0) {
                [_tableView.mj_footer endRefreshing];
                _tableView.mj_footer.state = MJRefreshStateNoMoreData;
                return ;
            }
            for (NSDictionary *dic in listArray) {
                S1mpl3Album *album = [[S1mpl3Album alloc] init];
                [album setValuesForKeysWithDictionary:dic];
                [_dataSourceArray addObject:album];
            }
            [_tableView reloadData];
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
        }];
    }else{
//            NSLog(@"%@", [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/tags/get_sounds?device=iPhone&page=1&sort=hot&tname=%@", _titleString]);
        [TwoToOne getDataByString:[NSString stringWithFormat:@"http://mobile.ximalaya.com/m/tags/get_sounds?device=iPhone&page=%ld&sort=hot&tname=%@", _page, _titleString] BodyString:nil WithDataBlock:^(id data) {
            if ([[data  objectForKey:@"list"]count] == 0) {
                [_tableView.mj_footer endRefreshing];
                _tableView.mj_footer.state = MJRefreshStateNoMoreData;
                return ;
            }
            for (NSDictionary *dic in [data objectForKey:@"list"]) {
                [_dataSourceArray addObject:dic];
            }
            [_tableView reloadData];
            //        [_tableView.header endRefreshing];
            [_tableView.mj_footer endRefreshing];
        }];

    }
}

-(void)loadView{
    [super loadView];
    _page = 1;
    self.dataSourceArray = [NSMutableArray array];
    

    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30 * FITHeight, self.view.frame.size.width, self.view.frame.size.height - 64 * FITHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20 * FITHeight)];
    _segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"声音", @"专辑"]];
    _segmentedControl.frame = CGRectMake(50 * FITWidth, 0, 275 * FITWidth, 30 * FITHeight);
    _segmentedControl.selectedSegmentIndex = 1;
    [_segmentedControl setTintColor:[UIColor orangeColor]];
//    [_tableView addSubview:_segmentedControl];
    [_segmentedControl addTarget:self action:@selector(segmentAction) forControlEvents:UIControlEventValueChanged];
    [view addSubview:_segmentedControl];
//    _tableView.tableHeaderView = view;
    [self.view addSubview:_segmentedControl];
    [self getData];
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataSourceArray removeAllObjects];
        _page = 1;
        [self getData];
    }];
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getData];
    }];

}

-(void)segmentAction{
//    NSLog(@"%ld", _segmentedControl.selectedSegmentIndex);
    if (_segmentedControl.selectedSegmentIndex == 1) {
        [_dataSourceArray removeAllObjects];
        [self getData];
    }else{
        [_dataSourceArray removeAllObjects];
        [self getData];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _titleString;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark--tableView代理方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_segmentedControl.selectedSegmentIndex == 1) {
        static NSString *identifier = @"cell";
        S1mpl3RecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[S1mpl3RecommendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            
        }
        cell.album = [_dataSourceArray objectAtIndex:indexPath.row];
        
        return cell;
    }else{
        static NSString *identifier1 = @"cell1";
        S1mpl3AlbumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier1];
        if (!cell) {
            cell = [[S1mpl3AlbumTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier1];
            
        }
        cell.videoDic = [_dataSourceArray objectAtIndex:indexPath.row];
        
        return cell;

    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_segmentedControl.selectedSegmentIndex == 1) {
        return 100 * FITHeight;
    }else
        return 130 * FITHeight;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSourceArray.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_dataSourceArray.count == 0) {
        return;
    }
    if (_segmentedControl.selectedSegmentIndex == 1) {
        S1mpl3AlbumViewController *albumVC = [[S1mpl3AlbumViewController alloc] init];
        albumVC.albumId = [[_dataSourceArray objectAtIndex:indexPath.row] ID];
        albumVC.myTitle = [[_dataSourceArray objectAtIndex:indexPath.row] title];
        [self.navigationController pushViewController:albumVC animated:YES];
    }else{
    
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
